﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using lcomixcore;

namespace comix.admininstration
{
    public partial class uploadbook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpPostedFile file = Request.Files["Filedata"];
            int id = Convert.ToInt32(Request.Params["BID"]);
            string fn = Request.MapPath("books/" + id.ToString()+ ".cmk");
            if (File.Exists(fn))
                File.Delete(fn);
            file.SaveAs(fn);
            CreateSmallThumbnail(id);
            CreateLargeThumbnail(id);
        }

        protected void CreateSmallThumbnail(int id)
        {
            string fn = Request.MapPath("thumbs/small/" + id.ToString() + ".jpg");
            string bookpath = Request.MapPath("books/" + id.ToString() + ".cmk");
            if (File.Exists(bookpath))
            {
                lBook book = new lBook();
                book.Open(bookpath);
                lRbook rbook = book.Rbook;
                int h = 130;
                int w = (int)((float)(rbook.Width * h) / rbook.Height);
                System.Drawing.Size sz = new System.Drawing.Size(w, h);
                lRpage pg = (lRpage)rbook.Pages[0].getResolved();
                System.Drawing.Bitmap b = pg.GetThumbnailOfPage(sz);

                
               

                int aw = 80;
                int ah = 130;
                if (b.Width > 80)
                    ah = b.Height * 80 / b.Width;
                else
                    aw = b.Width * 130 / b.Height;
                
                Bitmap b2 = new Bitmap(aw, ah);
                Graphics g = Graphics.FromImage(b2);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                Rectangle rsrc = new Rectangle(0, 0, b.Width, b.Height);
                Rectangle rdest = new Rectangle(0, 0, aw, ah);
                g.DrawImage(b, rdest, rsrc, GraphicsUnit.Pixel);
                if (File.Exists(fn))
                    File.Delete(fn);
                b2.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg);
                g.Dispose();
                b.Dispose();
                b2.Dispose();
            }
        }

        protected void CreateLargeThumbnail(int id)
        {
            string fn = Request.MapPath("thumbs/large/" + id.ToString() + ".jpg");
            string bookpath = Request.MapPath("books/" + id.ToString() + ".cmk");
            if (File.Exists(bookpath))
            {
                lBook book = new lBook();
                book.Open(bookpath);
                lRbook rbook = book.Rbook;
                int h = 220;
                int w = (int)((float)(rbook.Width * h) / rbook.Height);
                System.Drawing.Size sz = new System.Drawing.Size(w, h);
                lRpage pg = (lRpage)rbook.Pages[0].getResolved();
                System.Drawing.Bitmap b = pg.GetThumbnailOfPage(sz);

   

                int aw = 135;
                int ah = 220;
                if (b.Width > 135)
                    ah = b.Height * 135 / b.Width;
                else
                    aw = b.Width * 220 / b.Height;
                

                Bitmap b2 = new Bitmap(aw, ah);
                Graphics g = Graphics.FromImage(b2);
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                Rectangle rsrc = new Rectangle(0, 0, b.Width, b.Height);
                Rectangle rdest = new Rectangle(0, 0, aw, ah);
                g.DrawImage(b, rdest, rsrc, GraphicsUnit.Pixel);
                if (File.Exists(fn))
                    File.Delete(fn);
                b2.Save(fn, System.Drawing.Imaging.ImageFormat.Jpeg);
                g.Dispose();
                b.Dispose();
                b2.Dispose();
              
            }
        }
    }
}
